package entities;

import java.awt.geom.Rectangle2D;

import menu.Settings;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;

import util.Vector2D;

public class LarvaBoss extends Enemy {

	private float dRot;
	private float maxRot;

	private int time;

	private int filterTime;

	// private int attackPhaseTimer;
	private int attackPhaseTimeout;

	int phase;
	int numPhases = 3;

	private boolean vulnerable;

	private Vector2D[] targetLocations;
	private int currentTargetLocation;

	private float phase1Speed;

	private float shotTimeout;
	private float aimTime;

	private int healthbarWidth = 200;
	private int healthbarHeight = 20;

	public LarvaBoss() throws SlickException {
		super("res/larvaboss.png");
//		image = image.getScaledCopy(.55f);
		score = 10000;
		maxHealth = (int) (100 * Settings.getfactor());
		health = maxHealth;
		maxRot = 1000.0f;
		dRot = maxRot;
		time = 0;
		phase = 0;
		targetLocations = new Vector2D[4];
		targetLocations[0] = new Vector2D(700, 500);
		targetLocations[1] = new Vector2D(700, 100);
		targetLocations[2] = new Vector2D(100, 100);
		targetLocations[3] = new Vector2D(100, 500);
		phase1Speed = 300.0f;
		shotTimeout = 2000.0f;
		attackPhaseTimeout = 5000;
		aimTime = shotTimeout;
		this.reset();
	}

	@Override
	public Rectangle2D getHitbox() {
		float w = getImage().getWidth() * 0.4f;
		float h = getImage().getHeight() * 0.4f;
		return new Rectangle2D.Float(getX() + (w / 2.0f), getY() + (h / 2.0f),
				getImage().getWidth() - w, getImage().getHeight() - h);
	}

	@Override
	public void update(GameContainer gc, int delta) {
		super.update(gc, delta);
		// System.out.println(time);

		time += delta;

		if (filterTime > 0) {
			filterTime = Math.max(0, filterTime - delta);
		}

		switch (phase) {
		case 0:
			vulnerable = false;
			image.rotate(dRot * (delta / 1000.0f));
			if (time > 10000) {
				dRot = dRot * (1.0f - 0.999f * (delta / 1000.0f));
				dRot = dRot % 360.0f;
				System.out.println(dRot);
				if (dRot < 1.0f) {
					dRot = 0;
					// attackPhaseTimer = attackPhaseTimeout;
					phase = 1;
					time = 0;
					System.out.println("Phase 2 entered!");
				}

			}

			Vector2D distance = targetLocations[currentTargetLocation]
					.subtract(center());
			velocity = distance.getUnitVector().scale(phase1Speed);
			if (distance.getLength() < 5) {
				currentTargetLocation += random
						.nextInt(targetLocations.length - 1) + 1;
				currentTargetLocation = currentTargetLocation
						% targetLocations.length;
			}

			try {
				if (aimTime <= 0) {
					LarvaBeam beam = new LarvaBeam(this,
							level.getPlayer().pos.subtract(this.pos),
							level.getPlayer());
					beam.spawn(level, center());
					aimTime = shotTimeout;
				}
				aimTime -= delta;
			} catch (SlickException e) {
				e.printStackTrace();
			}
			break;
		case 1:
			vulnerable = true;
			velocity = Vector2D.ZERO_VECTOR;
			// System.out.println("Velocity: " + velocity.getLength());
			if (time >= attackPhaseTimeout) {
				// attackPhaseTimer = attackPhaseTimeout;
				dRot = maxRot;
				time = 0;
				phase1Speed += 75;
				shotTimeout = shotTimeout * 0.75f;
				phase = 0;
			}
			// System.out.println(velocity.getLength());
			break;
		default:
			break;
		}
	}

	@Override
	public void render(GameContainer gc, Graphics g) {
		if (filterTime > 0) {
			image.draw(pos.getX(), pos.getY(), Color.red);
		} else {
			super.render(gc, g);
		}
		Vector2D barPos = new Vector2D(gc.getWidth() / 2 - healthbarWidth / 2,
				50);
		g.setColor(new Color(0x0c1a35));
		g.fillRect(barPos.getX(), barPos.getY(), healthbarWidth, 20);
		g.setColor(new Color(0x890000));
		g.fillRect(barPos.getX() + 1, barPos.getY() + 1,
				(healthbarWidth - 2.0f) * (((float) health) / maxHealth),
				healthbarHeight - 2);
	}

	@Override
	public void takeDamage(int damage) {
		if (vulnerable) {
			System.out.println("Health: " + health);
			super.takeDamage(damage);
			filterTime = 100;
		}
	}

	@Override
	public String getName() {
		return "LarvaBoss";
	}

	@Override
	public void reset() {
		super.reset();
	}

}
